package org.video.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.flexbox.FlexboxLayout;

import org.greenrobot.eventbus.EventBus;
import org.video.mine.R;
import org.video.mine.Urls;
import org.video.mine.activity.PlayerActivity;
import org.video.mine.databinding.DetailLoadBinding;
import org.video.mine.databinding.MovieListLayoutBinding;
import org.video.mine.databinding.PlayListLayoutBinding;
import org.video.mine.entity.MoviePlayerEntity;
import org.video.mine.entity.PlayListEntity;
import org.video.mine.event.DownDetailEvent;
import org.video.mine.utils.ScreenUtils;

import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.ViewHolder> {

    List<PlayListEntity> listEntities;
    Context context;
    boolean isLoading = false;
    private int loadingIndexSet = -1;
    private int loadingIndexPosition = -1;

    public PlayListAdapter(List<PlayListEntity> listEntities, Context context) {
        this.listEntities = listEntities;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.movie_list_layout, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        MovieListLayoutBinding binding = (MovieListLayoutBinding) holder.getTitleBinding();
        binding.tvDownAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new DownDetailEvent(position));
            }
        });
        binding.setPlayList(listEntities.get(position));
        initFlexBox(binding,listEntities.get(position),position);
    }

    @Override
    public int getItemCount() {
        return listEntities.size();
    }

    private void initFlexBox(MovieListLayoutBinding binding, PlayListEntity playListEntity, int position){
        List<MoviePlayerEntity> playLists = playListEntity.getPlayLists();
        binding.flexItem.removeAllViews();
        if (!isLoading){
            binding.flexItem.setJustifyContent(FlexboxLayout.JUSTIFY_CONTENT_FLEX_START);
        }else {
            binding.flexItem.setJustifyContent(FlexboxLayout.JUSTIFY_CONTENT_CENTER);
        }
        for (int i=0; i<playLists.size(); i++){
            binding.flexItem.addView(createNewFlexItemTextView(i,playLists,position));
        }
    }

    private View createNewFlexItemTextView(final int index, final List<MoviePlayerEntity> playListEntity, final int position) {
        final MoviePlayerEntity moviePlayerEntity = playListEntity.get(index);
        LinearLayout ll = new LinearLayout(context);
        DetailLoadBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.detail_load,ll,false);
        if (!isLoading){
            binding.ivDown.setVisibility(View.GONE);
        }else {
            binding.ivDown.setVisibility(View.VISIBLE);
            if (loadingIndexSet==index && loadingIndexPosition == position){
                binding.ivDown.setImageResource(R.drawable.download);
            }else {
                binding.ivDown.setImageResource(R.drawable.download_gray);
            }
        }
        binding.tvName.setText(moviePlayerEntity.getName());
        binding.rlShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isLoading) {
                    Bundle bundle = new Bundle();
                    bundle.putString("url", moviePlayerEntity.getUrl().startsWith("http")?moviePlayerEntity.getUrl():(Urls.getHomeUrl0() + moviePlayerEntity.getUrl()));
//                bundle.putSerializable("play", (Serializable) playerEntities);
                    bundle.putInt("which", index);
                    bundle.putInt("position", position);
                    bundle.putString("imgUrl", listEntities.get(position).getMovieDetail().getImgUrl());
                    bundle.putString("name", listEntities.get(position).getMovieDetail().getName());
                    bundle.putInt("time", 0);
                    Intent intent = new Intent(context, PlayerActivity.class);
                    intent.putExtra("bundle", bundle);
                    context.startActivity(intent);
                }else {
                    if (loadingIndexPosition!=position && loadingIndexSet != index) {
                        loadingIndexSet = index;
                        loadingIndexPosition = position;
                        notifyItemChanged(position);
                    }
                }
            }
        });
        ViewCompat.setPaddingRelative(binding.getRoot(), 30, 8, 30, 8);
        FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        int margin = ScreenUtils.dp2px(6);
        int marginTop =ScreenUtils.dp2px(5);
        int bottom =ScreenUtils.dp2px(3);
        layoutParams.setMargins(margin, marginTop, margin, bottom);
        binding.getRoot().setLayoutParams(layoutParams);
        return binding.getRoot();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ViewDataBinding titleBinding = null;

        public ViewHolder(ViewDataBinding titleBinding) {
            super(titleBinding.getRoot());
            this.titleBinding = titleBinding;
        }

        public ViewDataBinding getTitleBinding(){
            return titleBinding;
        }
    }

    public void setLoading(boolean load){
        this.isLoading = load;
    }

    public boolean getLoading(){
        return isLoading;
    }
}
